package br.com.iswe.gwt.ui.client;

import br.com.iswe.gwt.util.client.ClientUtils;
import br.com.iswe.gwt.util.client.FlatMap;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;

public class CellContainerLongTextBuilder extends CellContainerBuilder {

	private static final String PAINEL_HINT = "painel-hint";
	protected int tamanhoMaximo;
	protected String width = "200px";
	protected int leftDiff = 80;
	
	public CellContainerLongTextBuilder(int tamanhoMaximo){
		this.tamanhoMaximo = tamanhoMaximo;
	}
	
	public CellContainerLongTextBuilder(int tamanhoMaximo, String width, int leftDiff){
		this.tamanhoMaximo = tamanhoMaximo;
		this.width = width;
		this.leftDiff = leftDiff;
	}
	
	public CellContainer build(int row, int col, FlatMap bean, ColumnInfo colInfo) {
		String texto = bean.getString(colInfo.getPropriedade());
		//verifica o tamanho do historico.
		if( ClientUtils.isNotEmpty(texto) ){
			if(texto.length() > tamanhoMaximo){
				final PopupPanel popUpTexto = new PopupPanel(true);
				DockPanel dockPanel = new DockPanel();
				dockPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
				dockPanel.add(new Label(":: " + colInfo.getCaption() + " ::"), DockPanel.NORTH);
				dockPanel.add(new Label(texto), DockPanel.CENTER);
				dockPanel.setWidth(this.width);
				dockPanel.setHeight("80px");
				dockPanel.setStyleName(PAINEL_HINT);
				popUpTexto.setWidget(dockPanel);

				texto = texto.substring(0, tamanhoMaximo - 1) + "...";
				Hyperlink rateio = new Hyperlink(texto, "HINT");
				rateio.setTitle(ClientUtils.commonConstants.mostrarTextoComleto());
				rateio.addClickListener(new ClickListener(){public void onClick(Widget sender) {
					popUpTexto.show();
					int left = sender.getAbsoluteLeft() - leftDiff;
				    int top  = sender.getAbsoluteTop() - 30;
				    if(left < 0){
				    	left = 30;
				    }
				    popUpTexto.setPopupPosition(left, top);
					//ClientUtils.centerPopUp(popUpRateio);
				}});
				return new CellContainer(rateio);
			}
		}
		return new CellContainer(texto);
	}
	
}
